﻿Imports System.IO

Public Class WatchDog
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        WatchList.ShowDialog()
    End Sub
    Private WithEvents watcher As New FileSystemWatcher()
    'Private Const MonitorDirectory As String = "D:\WorkspaceForCuidh\TestFolder" ' 替謐｢荳ｺ菴要逶題ｧ・I目蠖・ 
    Private MonitorDirectory As String = Application.StartupPath ' 替謐｢荳ｺ菴要逶題ｧ・I目蠖・ 
    'Private Const TargetFileName As String = "1111.txt" ' 替謐｢荳ｺ菴要逶題ｧ・I特定文件名，包括謇ｩ展名  

    Private iniHelper As IniFileHelper
    Private listFiles As ArrayList
    Private fileNames As ArrayList


    Private Sub WatchDog_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        iniHelper = New IniFileHelper(Application.StartupPath & "\WatchList.ini")
        listFiles = iniHelper.listIniFiles
        fileNames = GetFileNames(listFiles)


        watcher.Path = MonitorDirectory
        watcher.IncludeSubdirectories = True
        watcher.NotifyFilter = NotifyFilters.LastWrite Or NotifyFilters.FileName Or NotifyFilters.DirectoryName
        watcher.Filter = "*.*" ' 逶題ｧ・蒲L文件，如果需要特定謇ｩ展名可以更改霑剽｢的通配符  
        watcher.EnableRaisingEvents = True ' 蜷ｯ用事件触蜿・ 
    End Sub

    Private Function GetFileNames(ByVal list As ArrayList) As ArrayList
        Dim listResult As New ArrayList
        For Each item As INIFile In list
            listResult.Add(item.FileName)
        Next

        GetFileNames = listResult
    End Function

    ' 当文件或目蠖蕪I名称蜿草ｶ更改譌ｶ触蜿蒼I事件  
    Private Sub watcher_Renamed(sender As Object, e As RenamedEventArgs) Handles watcher.Renamed
        If fileNames.Contains(e.Name) Then
            ' 文件重命名后名称匹配，蜿荘酪ﾁ息扈剽p謌ｷ  
            SendMessageToUser("File with target name " + MonitorDirectory + e.Name + " has been renamed.")
        End If
    End Sub

    ' 当文件或目蠖部淲・建譌ｶ触蜿蒼I事件  
    Private Sub watcher_Created(sender As Object, e As FileSystemEventArgs) Handles watcher.Created
        If fileNames.Contains(e.Name) Then
            ' 新建文件名称匹配，蜿荘酪ﾁ息扈剽p謌ｷ  
            SendMessageToUser("File with target name " + MonitorDirectory + e.Name + " has been created.")
        End If
    End Sub

    Private Sub watcher_Changed(sender As Object, e As FileSystemEventArgs) Handles watcher.Changed

        Dim fileName As String
        If e.Name.IndexOf("\") > 0 Then
            fileName = e.Name.Substring(e.Name.IndexOf("\") + 1)
        Else
            fileName = e.Name
        End If

        If fileNames.Contains(fileName) Then
            ' 新建文件名称匹配，蜿荘酪ﾁ息扈剽p謌ｷ  
            SendMessageToUser("ファイル [" + MonitorDirectory + e.Name + "]" + vbCrLf + GetMessage(fileName, IniFileHelper.WatchType.Change))
        End If


    End Sub

    ' 蜿荘酪ﾁ息扈剽p謌ｷ的函数（霑剽｢只是一个示例，菴可能需要根据菴的蠎迫p程序邀ｻ型来螳樒鴫具体的消息蜿荘鈴ｻ霎早j  
    Private Sub SendMessageToUser(message As String)

        'MessageBox.Show(message, "File Watcher Notification", MessageBoxButtons.OK, MessageBoxIcon.Information)

        'Dim thd As New Threading.Thread(AddressOf PopupMessageWindow)
        'thd.Start()
        PopupMessageWindow(message)
    End Sub

    Private Sub PopupMessageWindow(ByVal msg As String)

        Me.Invoke(New Action(Sub()
                                 Dim myDialog As New MessageWindow()
                                 myDialog.Label1.Text = msg
                                 myDialog.TopMost = True
                                 myDialog.ShowDialog(Me)
                             End Sub))

    End Sub

    Private Function GetMessage(ByVal filename As String, ByVal watchType As Integer) As String
        Dim currentItem As New INIFile
        For Each item As INIFile In listFiles

            If filename.Equals(item.FileName) Then
                currentItem = item
                Exit For
            End If
        Next

        Dim msg As String = ""

        Select Case watchType
            Case IniFileHelper.WatchType.Create
                If Not "".Equals(Trim(currentItem.CreateMessage)) Then
                    msg = currentItem.CreateMessage
                Else
                    msg = "Was Been Created !"
                End If
            Case IniFileHelper.WatchType.Rename
                If Not "".Equals(Trim(currentItem.RenameMessage)) Then
                    msg = currentItem.RenameMessage
                Else
                    msg = "Was Been Renamed !"
                End If
            Case IniFileHelper.WatchType.Delete
                If Not "".Equals(Trim(currentItem.DeleteMessage)) Then
                    msg = currentItem.DeleteMessage
                Else
                    msg = "Was Been Deleted !"
                End If
            Case IniFileHelper.WatchType.Change
                If Not "".Equals(Trim(currentItem.ChangeMessage)) Then
                    msg = currentItem.ChangeMessage
                Else
                    msg = "Was Been Changed !"
                End If
        End Select

        GetMessage = msg
    End Function


    Private Sub MyFormHide()

        Me.Hide()
        Me.NotifyIcon1.Visible = True

    End Sub
    'Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, _
    ' ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
    Private Sub MyFormShow()

        Me.Show()
        Me.NotifyIcon1.Visible = False
        'Me.Location = pPopLocation
        'Me.Size = pPopSize


    End Sub

    Private Sub WatchDog_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        e.Cancel = True
        Me.MyFormHide()
    End Sub

    Private Sub NotifyIcon1_MouseClick(sender As Object, e As MouseEventArgs) Handles NotifyIcon1.Click
        Me.MyFormShow()
    End Sub
End Class
